from netfilterqueue import NetfilterQueue
from scapy.compat import raw
from scapy.layers.inet import IP, TCP
from scapy.packet import NoPayload


def print_and_accept(pkt):
    scapy_pkt = IP(pkt.get_payload())
    scapy_pkt.show()
    if type(scapy_pkt[TCP].payload) != NoPayload:  # null 修改
        scapy_pkt[TCP].remove_payload()
        scapy_pkt[TCP].add_payload("abc")
    del scapy_pkt[IP].chksum
    del scapy_pkt[IP].len
    del scapy_pkt[TCP].chksum
    scapy_pkt = IP(raw(scapy_pkt))
    scapy_pkt.show()
    pkt.set_payload(bytes(scapy_pkt))
    pkt.accept()


queue = NetfilterQueue()
queue.bind(1, print_and_accept, max_len=8192)
try:
    queue.run()
except KeyboardInterrupt:
    queue.unbind()
